import axios from "axios"
import Vue from "vue"
import router from "../router"
import qs from "querystring"
import { Toast } from 'vant';
import exp from "constants";

//环境配置
if (process.env.NODE_ENV == "development") {
    // 开发环境
    Vue.prototype.$pre = "http://localhost:3000"
} else {
    // 生产环境
    Vue.prototype.$pre = ""
}

axios.interceptors.response.use((res) => {
    //1.打印数据
    console.log("响应拦截");
    console.log("本次请求的地址是：" + res.config.url);
    console.log(res);
    // 2.统一处理失败
    if (res.data.code !== 200) {
        Toast(res.data.msg)
    }
    //3.掉线处理
    if (res.data.msg == "登录已过期或访问权限受限") {
        //跳转到登录页
        router.push("/login")
    }
    return res;
})

//登录请求
export let reqLogin = (user) => {
    return axios({
        url: "/api/login",
        method: "post",
        /*data:{
          phone:this.user.phone,
          password:this.user.password
        }*/
        data: user
    })
}

//注册请求
export let reqRegister = (user) => {
    return axios({
        url: "/api/register",
        method: "post",
        data: user
    })
}

// 首页分类列表数据
export let reqHomeCates = () => {
    return axios({
        url: "/api/getcate",
        method: "get"
    })
}

//分类列表页
export let reqList = (parmas) => {
    return axios({
        url: "/api/getgoodlist",
        method: "get",
        params: parmas
    })
}

//搜索
export let reqSearch = (params) => {
    return axios({
        url: "/api/search",
        method: "get",
        params: params
    })
}

//详情
export let reqDetail = (params) => {
    return axios({
        url: "/api/getgoodsinfo",
        method: "get",
        params: params
    })
}

//加入购物车
export let reqAddShop = (d) => {
    return axios({
        url: "/api/cartadd",
        method: "post",
        data: d,
        params: {
            uid: JSON.parse(localStorage.getItem("user")).uid,
        },
    })
}

//轮播
export let reqBanner = () => {
    return axios({
        url: "/api/getbanner"
    })
}

//地址列表
export let reqaddressList = (parmas) => {
    return axios({
        url: "/api/addresslist",
        methods: "get",
        params: parmas
    })
}